package com.simdo.g768.Adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.simdo.g768.MainActivity;
import com.simdo.g768.R;
import com.simdo.g768.Util.GsonUtil;
import com.simdo.g768.Util.PrefUtil;

import java.util.List;

public class CellManageRecyclerviewAdapter extends RecyclerView.Adapter<CellManageRecyclerviewAdapter.ViewHolder> {
    Context context;
    private final List<String> arfcnList;
    int cell;

    public CellManageRecyclerviewAdapter(Context context, List<String> arfcnList, int cell) {
        this.context = context;
        this.arfcnList = arfcnList;
        this.cell = cell;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_update_arfcn_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final int pos = position;
        holder.arfcn.setText(arfcnList.get(pos));

        holder.btn_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String removed = arfcnList.remove(pos);
                MainActivity.getInstance().cellMap.get(cell).remove(removed);
                notifyDataSetChanged();
                PrefUtil.build().putString("cell_manage", GsonUtil.listMapToJson(MainActivity.getInstance().cellMap));
            }
        });
    }

    @Override
    public int getItemCount() {
        return arfcnList.size();
    }

    @SuppressLint("NotifyDataSetChanged")
    public void clear() {
        arfcnList.clear();
        notifyDataSetChanged();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView arfcn;
        ImageView btn_del;

        public ViewHolder(View view) {
            super(view);
            arfcn = view.findViewById(R.id.arfcn);
            btn_del = view.findViewById(R.id.btn_del);
        }
    }
}
